package com.weixinli.web.actions;

import java.util.Map;
import java.util.Random;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.weixinli.beans.DetailInfo;
import com.weixinli.beans.UserInfo;
import com.weixinli.biz.IDetailInfoBiz;
import com.weixinli.biz.IUserInfoBiz;
import com.weixinli.encrypt.MD5Encryption;
import com.weixinli.utils.MyTimer;
import com.weixinli.utils.SessionAttributeName;
import com.weixinli.web.actions.model.JsonObject;

@Scope("prototype")
@Controller("userInfoAction")
public class UserInfoAction extends ActionSupport implements SessionAware{
	private UserInfo userInfo;
	
	@Resource(name="iUserInfoBiz")
	private IUserInfoBiz userInfoBiz;
	@Resource(name="iDetailInfoBiz")
	private IDetailInfoBiz detailInfoBiz;
	
	private JsonObject jsonObject;
	
	private Map<String,Object> session;
	
	private String checkNum;
	private JavaMailSender mailSender;
	
	/**
	 * 注册
	 */
	public String register(){
		jsonObject=new JsonObject();
		Object obj=session.get(SessionAttributeName.CHECKCODE);
		if(obj==null){
			jsonObject.setResult(0);
		}else if(checkNum.equals(obj)){
			userInfo.setW_pwd(MD5Encryption.createPassword(userInfo.getW_pwd()));
			if(userInfoBiz.addUserInfo(userInfo)>0){
				session.put(SessionAttributeName.MARKINFO,"success");   //注册成功,则存起来 
				session.put(SessionAttributeName.CHECKCODE, null);
				session.remove(SessionAttributeName.CHECKCODE);
				jsonObject.setResult(1);
			}else{
				jsonObject.setResult(2);
			}
		}else{
			jsonObject.setResult(2);
		}
		return "success";
	}
	
	/**
	 * 发送邮件
	 */
	public String sendCode(){
		jsonObject=new JsonObject();
		Random random=new Random();
		String st="";
		while(st.length()<6)
		{
			st+=random.nextInt(10);
		}
		session.put(SessionAttributeName.CHECKCODE,st); 
		System.out.println(st+"             ffffffffff");
		sendEmailMessage(userInfo,st);
		MyTimer myTimer=new MyTimer(); 
		myTimer.getTimeer(session);
		jsonObject.setResult(1);
		return "success";
	}
	
	/**
	 * 验证用户是否存在过
	 * @return
	 */
	
	public String registerTest(){
		jsonObject=new JsonObject();
		UserInfo uf=userInfoBiz.userTest(userInfo);
		if(uf==null){
			jsonObject.setResult(1); //邮箱没有注册过
		}else{
			jsonObject.setResult(0);
		}
		return "success";
	}
	
	//登录的方法
	public String login(){
		jsonObject =new JsonObject();
		userInfo.setW_pwd(MD5Encryption.createPassword(userInfo.getW_pwd()));
		UserInfo uf=userInfoBiz.userLogin(userInfo);
		if(uf==null) {
			jsonObject.setResult(0); //用户名或密码错误
		}else{
			DetailInfo di = detailInfoBiz.getOneDeInfo(uf.getW_uid());
			System.out.println("******************************************"+di);
			session.put(SessionAttributeName.CURRENTUSER, uf);  //登录后的把用户存到session中
			session.put(SessionAttributeName.USERDETAILINFO, di);
			jsonObject.setObj(uf);
			jsonObject.setResult(1);
		}
		return "success";
	}
	
	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}
	
	public UserInfo getUserInfo() {
		return userInfo;
	}

	public void setUserInfo(UserInfo userInfo) {
		this.userInfo = userInfo;
	}

	public void setUserInfoBiz(IUserInfoBiz userInfoBiz) {
		this.userInfoBiz = userInfoBiz;
	}

	public JsonObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JsonObject jsonObject) {
		this.jsonObject = jsonObject;
	}
	public void setCheckNum(String checkNum) {
		this.checkNum = checkNum;
	}
	
	@Resource(name="mailSender")
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	/**
	 * 发送邮件
	 * @param userInfo
	 * @param Code
	 */
	public void sendEmailMessage(final UserInfo userInfo,final String Code){
		MimeMessagePreparator preparator=new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) throws Exception {
				MimeMessageHelper message=new MimeMessageHelper(mimeMessage,"utf-8");
				message.setSubject(Code);
				message.setTo(userInfo.getW_email());
				message.setFrom("15616696243@163.com");
				String text="微心理注册的验证码，两分钟内有用:";
				message.setText(text+Code);
			}
		};
		this.mailSender.send(preparator);
	}
}
